﻿define(['util', 'ajax'], function (util, ajax) {
    "use strict";

    var exports = {},
         ViewModel;

    ViewModel = function (data) {
        var self = this;

        self.name = ko.observable();
        self.address = ko.observable();
        self.email = ko.observable();
        self.mobile = ko.observable();

        self.dateOfBirth = ko.observable();

        self.tagId = ko.observable();
        self.code = ko.observable();

        self.validFrom = ko.observable();
        self.validTo = ko.observable();

        self.screenMessage = ko.observable();

        self.sendEmail = ko.observable();
        self.sendSms = ko.observable();
        self.usePrint = ko.observable();
        self.block = ko.observable();

        self.roles = ko.observableArray(data.roles);
        self.selectedRole = ko.observable();

        self.userStatuses = ko.observableArray(data.userStatuses);
        self.selectedStatus = ko.observable();

        //photo Upload Section
        self.imageData = ko.observable();

        //Compatibility
        self.tempImage = ko.observable();

        self.tempImage.subscribe(function (newValue) {
            var canvas = document.getElementById("imgCanvas");
            util.image.drawImage(canvas, newValue);
            self.imageData(canvas.toDataURL());
        });

        self.tempImage(util.image.checkOrDefault(self.imageData));

        self.selectPhoto = function () {
            $("#inputFile").click();
        };

        self.capturePhoto = function () {
            $("#inputFile").click();
        };

        $.subscribe('filesDropped', function (files) {

            if (files.length == 0) return;
            var fileToLoad = files[0];
            var fileReader = new FileReader();
            fileReader.onload = function (fileLoadedEvent) {
                self.tempImage(fileLoadedEvent.target.result);
            };
            fileReader.readAsDataURL(fileToLoad);
        });


        self.save = function () {
            var role = _.find(self.roles(), function (val) {
                return val.name == self.selectedRole();
            });
            var status = _.find(self.userStatuses(), function (val) {
                return val.name == self.selectedStatus();
            });
            var form = {
                TagId: self.tagId(),
                Code: self.code(),
                Name: self.name(),
                DateOfBirth: moment(self.dateOfBirth()).toISOString(),
                Address: self.address(),
                Email: self.email(),
                Mobile: self.mobile(),
                ValidFrom: moment(self.validFrom()).toISOString(),
                ValidTo: moment(self.validTo()).toISOString(),
                SendSms: self.sendSms() == undefined ? false : self.sendSms(),
                SendEmail: self.sendEmail() == undefined ? false : self.sendEmail(),
                UsePrint: self.usePrint() == undefined ? false : self.usePrint(),
                Block: self.block() == undefined ? false : self.block(),
                RoleValue: role == undefined ? '' : role.id,
                UserStatusValue: status == undefined ? '' : status.id,
                ScreenMessage: self.screenMessage == undefined ? '' : self.screenMessage(),
                ImageData: self.imageData()
            };
            console.log(form);
            ajax.post('member', form).done(function (response) {
                if (response.id != '') {
                    window.location = '/member/show?id=' + response.id;
                }
            });
        };

        self.cancel = function () {
            window.location = "index";
        };
    };

    exports.exec = function (sectionId, model) {
        var viewModel = new ViewModel(model);
        ko.applyBindings(viewModel, document.getElementById(sectionId));
    };

    return exports;
});